﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using NetDon.OA.Data;
using NetDon.OA.Data.RelationClasses;
using NetDon.OA.Interface.CRM;
using NetDon.OA.Data.EntityClasses;
using NetDon.OA.Data.CollectionClasses;
using NetDon.OA.Data.HelperClasses;
using NetDon.OA.Data.Linq;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.WebControls;

namespace NetDon.OA.Business.CRM
{
    public class CustomerInfoService : ICustomerInfoService
    {
        public bool AddCustomerInfo(CustomerInfoData customerInfo)
        {
            var objCustomerInfo = new CustomerInfoEntity
            {
                CustomerId = customerInfo.CustomerId,
                CustomerCode = customerInfo.CustomerCode,
                CustomerName = customerInfo.CustomerName,
                Province = customerInfo.Province,
                City = customerInfo.City,
                Business = customerInfo.Business,
                CustomerSource = customerInfo.CustomerSource,
                ContactName = customerInfo.ContactName,
                ContactPosition = customerInfo.ContactPosition,
                Mobile = customerInfo.Mobile,
                Phone = customerInfo.Phone,
                Fax = customerInfo.Fax,
                Email = customerInfo.Email,
                Address = customerInfo.Address,
                Zipcode = customerInfo.Zipcode,
                Website = customerInfo.Website,
                Remark = customerInfo.Remark,
                Introduction = customerInfo.Introduction,
                Internation = customerInfo.Internation,
                IsChina = customerInfo.IsChina,
                AreaId = customerInfo.AreaId
            };
            bool result = objCustomerInfo.Save();
            return result;
        }

        public bool ModifyCustomerInfo(CustomerInfoData customerInfo)
        {
            var objCustomerInfo = new CustomerInfoEntity(customerInfo.CustomerId)
            {
                CustomerCode = customerInfo.CustomerCode,
                CustomerName = customerInfo.CustomerName,
                Province = customerInfo.Province,
                City = customerInfo.City,
                Business = customerInfo.Business,
                CustomerSource = customerInfo.CustomerSource,
                ContactName = customerInfo.ContactName,
                ContactPosition = customerInfo.ContactPosition,
                Mobile = customerInfo.Mobile,
                Phone = customerInfo.Phone,
                Fax = customerInfo.Fax,
                Email = customerInfo.Email,
                Address = customerInfo.Address,
                Zipcode = customerInfo.Zipcode,
                Website = customerInfo.Website,
                Introduction = customerInfo.Introduction,
                Internation = customerInfo.Internation,
                Remark = customerInfo.Remark,
                IsChina = customerInfo.IsChina,
                AreaId = customerInfo.AreaId
            };
            bool result = objCustomerInfo.Save();
            return result;
        }

        public bool RemoveCustomerInfo(Guid customerId)
        {
            var customerInfoCollection = new CustomerInfoCollection();
            var result = customerInfoCollection.DeleteMulti(CustomerInfoFields.CustomerId == customerId);

            return result > 0;
        }

        public CustomerInfoData GetCustomerInfoByCode(string clientCode)
        {
               var meta = new LinqMetaData();
            var customerInfo = (from c in meta.CustomerInfo
                        where c.CustomerCode == clientCode
                        select c).FirstOrDefault();
            return customerInfo == null
                       ? null
                       : new CustomerInfoData()
                             {
                                 CustomerId = customerInfo.CustomerId,
                                 CustomerCode = customerInfo.CustomerCode,
                                 CustomerName = customerInfo.CustomerName,
                                 Province = customerInfo.Province,
                                 City = customerInfo.City,
                                 Business = customerInfo.Business,
                                 CustomerSource = customerInfo.CustomerSource,
                                 ContactName = customerInfo.ContactName,
                                 ContactPosition = customerInfo.ContactPosition,
                                 Mobile = customerInfo.Mobile,
                                 Phone = customerInfo.Phone,
                                 Fax = customerInfo.Fax,
                                 Email = customerInfo.Email,
                                 Address = customerInfo.Address,
                                 Zipcode = customerInfo.Zipcode,
                                 Website = customerInfo.Website,
                                 Remark = customerInfo.Remark,
                                 Introduction = customerInfo.Introduction,
                                 Internation = customerInfo.Internation,
                                 IsChina = customerInfo.IsChina,
                                 AreaId = customerInfo.AreaId
                             };
            
        }

        public CustomerInfoData GetCustomerInfo(Guid customerId)
        {
            var meta = new LinqMetaData();
            var list = from customerInfo in meta.CustomerInfo
                       where customerInfo.CustomerId == customerId
                       select new CustomerInfoData
                       {
                           CustomerId = customerInfo.CustomerId,
                           CustomerCode = customerInfo.CustomerCode,
                           CustomerName = customerInfo.CustomerName,
                           Province = customerInfo.Province,
                           City = customerInfo.City,
                           Business = customerInfo.Business,
                           CustomerSource = customerInfo.CustomerSource,
                           ContactName = customerInfo.ContactName,
                           ContactPosition = customerInfo.ContactPosition,
                           Mobile = customerInfo.Mobile,
                           Phone = customerInfo.Phone,
                           Fax = customerInfo.Fax,
                           Email = customerInfo.Email,
                           Address = customerInfo.Address,
                           Zipcode = customerInfo.Zipcode,
                           Website = customerInfo.Website,
                           Remark = customerInfo.Remark,
                           Introduction = customerInfo.Introduction,
                           Internation = customerInfo.Internation,
                           IsChina = customerInfo.IsChina,
                           AreaId = customerInfo.AreaId
                       };
            return list.FirstOrDefault();
        }

        public IEnumerable<CustomerInfoData> GetAllCustomerInfo()
        {
            var collection = new CustomerInfoCollection();
            collection.GetMulti(null);
            return collection.Select(src => new CustomerInfoData()
                                                      {
                                                          CustomerCode = src.CustomerCode,
                                                          CustomerName = src.CustomerName,
                                                          CustomerId = src.CustomerId
                                                      });
        }

        public int GetCustomerInfoCount(CustomerInfoSearchCriteria criteria)
        {
            var collection = new CustomerInfoCollection();
            return collection.GetDbCount(GetSearchCustomerInfoPredicate(criteria));
        }

        public IEnumerable<CustomerInfoData> SearchCustomerInfoDatasByCondition(CustomerInfoSearchCriteria criteria, string sortExpression, System.Web.UI.WebControls.SortDirection sortDirection, int pageSize, int pageIndex)
        {
            var sort = GetSortExpression(sortExpression, sortDirection);
            var collection = new CustomerInfoCollection();
            collection.GetMulti(GetSearchCustomerInfoPredicate(criteria), 0, sort,
                              null, pageIndex + 1, pageSize);

            var result = from customerInfo in collection
                         select new CustomerInfoData
                         {
                             CustomerId = customerInfo.CustomerId,
                             CustomerCode = customerInfo.CustomerCode,
                             CustomerName = customerInfo.CustomerName,
                             Province = customerInfo.Province,
                             City = customerInfo.City,
                             Business = customerInfo.Business,
                             CustomerSource = customerInfo.CustomerSource,
                             ContactName = customerInfo.ContactName,
                             Mobile = customerInfo.Mobile,
                             Phone = customerInfo.Phone,
                             Fax = customerInfo.Fax,
                             Email = customerInfo.Email,
                             Address = customerInfo.Address,
                             Zipcode = customerInfo.Zipcode,
                             Website = customerInfo.Website,
                             Remark = customerInfo.Remark,
                             Introduction = customerInfo.Introduction,
                             Internation = customerInfo.Internation,
                             IsChina = customerInfo.IsChina,
                             AreaId = customerInfo.AreaId
                         };

            return result;
        }

        private SortExpression GetSortExpression(string strInput, SortDirection sortDirection)
        {
            SortExpression sort;
            switch (strInput)
            {
                case "CustomerCode":
                    sort = new SortExpression(CustomerInfoFields.CustomerCode | (SortOperator)(int)sortDirection);
                    break;
                case "CustomerName":
                    sort = new SortExpression(CustomerInfoFields.CustomerName | (SortOperator)(int)sortDirection);
                    break;
                case "ContactName":
                    sort = new SortExpression(CustomerInfoFields.ContactName | (SortOperator)(int)sortDirection);
                    break;
                case "Phone":
                    sort = new SortExpression(CustomerInfoFields.Phone | (SortOperator)(int)sortDirection);
                    break;
                case "Mobile":
                    sort = new SortExpression(CustomerInfoFields.Mobile | (SortOperator)(int)sortDirection);
                    break;
                case "Email":
                    sort = new SortExpression(CustomerInfoFields.Email | (SortOperator)(int)sortDirection);
                    break;

                default:
                    sort = new SortExpression
                               {
                                   CustomerInfoFields.CustomerName | SortOperator.Ascending
                               };
                    break;

            }
            return sort;
        }

        private IPredicate GetSearchCustomerInfoPredicate(CustomerInfoSearchCriteria criteria)
        {
            var expression = new PredicateExpression();
            if (!string.IsNullOrEmpty(criteria.CustomerCode))
            {
                expression.Add(CustomerInfoFields.CustomerCode % ("%" + criteria.CustomerCode + "%"));
            }

            if (!string.IsNullOrEmpty(criteria.CustomerName))
            {
                expression.Add(CustomerInfoFields.CustomerName % ("%" + criteria.CustomerName + "%"));
            }
            if (!string.IsNullOrEmpty(criteria.ContactName))
            {
                expression.Add(CustomerInfoFields.ContactName % ("%" + criteria.ContactName + "%"));
            }
            //if (criteria.AreaId.HasValue)
            //{
            //    expression.Add(CustomerInfoFields.AreaId == criteria.AreaId);
            //}

            return expression;
        }


        public bool CheckCustomerCodeExist(Guid customerId, string customerCode, Guid areaId)
        {
            var meta = new LinqMetaData();
            bool exist = false;
            if (customerId != Guid.Empty)
            {
                //update check
                exist = meta.CustomerInfo.Any(a => a.CustomerId != customerId && a.CustomerCode.Equals(customerCode, StringComparison.OrdinalIgnoreCase) && a.AreaId == areaId);
            }
            else
            {
                //add check
                exist = meta.CustomerInfo.Any(a => a.CustomerCode.Equals(customerCode, StringComparison.OrdinalIgnoreCase) && a.AreaId == areaId);
            }
            return exist;
        }

        public bool CheckCustomerNameExist(Guid customerId, string customerName, Guid areaId)
        {
            var meta = new LinqMetaData();
            bool exist = false;
            if (customerId != Guid.Empty)
            {
                //update check
                exist = meta.CustomerInfo.Any(a => a.CustomerId != customerId && a.CustomerName.Equals(customerName, StringComparison.OrdinalIgnoreCase) && a.AreaId == areaId);
            }
            else
            {
                //add check
                exist = meta.CustomerInfo.Any(a => a.CustomerName.Equals(customerName, StringComparison.OrdinalIgnoreCase) && a.AreaId == areaId);
            }
            return exist;
        }

        public bool ValidateCustomerReferenced(Guid customerId)
        {
            var meta = new LinqMetaData();
            return (meta.IssuingOrder.Any(a => a.CustomerId == customerId));

        }

        public DictionaryCollection GetCustomerGoodsShippTypes(Guid customerId, Guid areaId)
        {
            var meta = new LinqMetaData();

            var collection = new DictionaryCollection();
            //collection.GetMulti(DictionaryFields.FieldValue == areaId &
            //                   CustomerGoodsShippingTypeFields.CustomerId == customerId, 0, null, GetCustomerGoodsShippTypeRelations());
            collection.GetMulti(CustomerGoodsShippingTypeFields.CustomerId == customerId, 0, null, GetCustomerGoodsShippTypeRelations());
            if(collection.Count<=0)
            {
                 //collection.GetMulti(DictionaryFields.FieldValue == areaId &
                 //              DictionaryCategoryFields.Name == "承运货物", 0, null, new RelationCollection(){ DictionaryEntity.Relations.DictionaryCategoryEntityUsingTypeId});

                 collection.GetMulti(DictionaryCategoryFields.Name == "承运货物", 0, null, new RelationCollection() { DictionaryEntity.Relations.DictionaryCategoryEntityUsingTypeId });
                
            }
            return collection;
        }

   
        private IRelationCollection GetCustomerGoodsShippTypeRelations()
        {
            var relation = new RelationCollection();
            relation.Add(new DynamicRelation(EntityType.CustomerGoodsShippingTypeEntity, JoinHint.Left,
                                             EntityType.DictionaryEntity, string.Empty, string.Empty,
                                             CustomerGoodsShippingTypeFields.AreaId == DictionaryFields.FieldValue & CustomerGoodsShippingTypeFields.GoodsShippedTypeId == DictionaryFields.Oid));
            relation.Add(new DynamicRelation(EntityType.CustomerGoodsShippingTypeEntity, JoinHint.Left,
                                             EntityType.CustomerInfoEntity, string.Empty, string.Empty,
                                             CustomerGoodsShippingTypeFields.AreaId == CustomerInfoFields.AreaId & CustomerGoodsShippingTypeFields.CustomerId == CustomerInfoFields.CustomerId));

            return relation;
        }


    }
}
